import React, { Component } from 'react'
import { connect } from 'dva';

import { Card, Form, Input, Row, Col, Button, Select, Checkbox, Radio, message } from 'antd'
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import CusInputNumber from '@/components/CusInputNumber';
const FormItem = Form.Item
@Form.create()
@connect(({ loading }) => ({ loading: loading.effects['product/putPresellDays'] }))
class PresellDays extends Component {
  state = {
    days: ''
  }
  componentDidMount() {
    this.queryPresellDays()
  }
  queryPresellDays = () => {
    const { dispatch } = this.props;
    dispatch({
      type: 'product/queryPresellDays',
    }).then(({ errorCode, data }) => {
      if (errorCode === 0) {
        this.setState({ days: data })
      }
    })
  }
  handleSubmit = () => {
    const { loading, dispatch, form: { validateFields } } = this.props;
    if (loading) return
    validateFields((err, value) => {
      if (err) return
      dispatch({
        type: 'product/putPresellDays',
        payload: {
          pathParams: {
            days: value.preSaleDays
          }
        }
      }).then(({ errorCode }) => {
        if (errorCode === 0) {
          message.success('设置预售天数成功')
        }
      })
    })
  }
  render() {
    const { form: { getFieldDecorator }, loading } = this.props;
    const { days } = this.state;
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 18 },
      },
    };
    return (
      <PageHeaderWrapper title='预售管理'>
        <Card bordered={false}>
          <Row>
            <Col span={16}>
              <FormItem label="预售天数" {...formItemLayout}>
                {getFieldDecorator('preSaleDays', {
                  rules: [
                    { required: true, message: '预售天数不能为空', whitespace: true },
                  ],
                  validateTrigger: 'onBlur',
                  initialValue: days,
                })(
                  <CusInputNumber
                    precision={0}
                    max={999}
                    min={1}
                    style={{ width: 264 }}
                  />
                )}
              </FormItem>
              <Row>
                <Col offset={6} span={18}>
                  <Button type='primary' loading={loading} style={{ width: 100 }} onClick={this.handleSubmit}>提交</Button>
                </Col>
              </Row>
            </Col>
          </Row>
        </Card>
      </PageHeaderWrapper>
    )
  }
}

export default PresellDays
